#include <libdswutility/dswutility.h>

CDswutility::CDswutility(LPST_ENV_T pEnv)
: CDswbase(pEnv)
, m_pTool_network(NULL)
, m_pDswrtcctrl(NULL)
, m_pWatchDog(NULL)
, m_pGPIO(NULL)
, m_pDisk(NULL)
, m_pPTZCtrl(NULL)
, m_pUpnpClient(NULL)
, m_pAvCtrl(NULL)
{
	LOG4CPLUS_DEBUG(log_dsw, "Create CDswutility class!");
	m_pTool_network = new CDswtool_network(pEnv);
	m_pDswrtcctrl = new CDswrtcctrl(pEnv);
	m_pWatchDog = new CDswwatchdog(pEnv);
	m_pGPIO = new CDswgpio(pEnv);
	m_pDisk = new CDswdisk(pEnv);
	m_pUpnpClient = new CDswupnpclient(pEnv);
	m_pAvCtrl = new CDswavctrl(pEnv);

	ST_PTZ_ATTR_T nPTZAttr;
	ST_CONSOLE_INFO_T nConInfo;
	SendMessage(PMSG_GET_PTZADDR_PARAM, &nPTZAttr);
	nConInfo.nbits = 8;
	nConInfo.nevent = 'N';
	nConInfo.nspeed = nPTZAttr.nBtl;
	nConInfo.nstop = 1;
	m_pPTZCtrl = new CDswPTZCtrl(pEnv, &nConInfo);

	ST_NET_PARAM_T nNetParam;
	SendMessage(PMSG_GET_ETH_PARAM, (void*)&nNetParam.nEthParam);
	SendMessage(PMSG_GET_DNS_PARAM, (void*)&nNetParam.nDnsParam);
	m_pTool_network->ApplyNetParam(&nNetParam);
}

CDswutility::~CDswutility()
{
	delete m_pTool_network;
	delete m_pWatchDog;
	delete m_pDswrtcctrl;
	delete m_pGPIO;
	delete m_pDisk;
	delete m_pPTZCtrl;
	delete m_pUpnpClient;
	delete m_pAvCtrl;
	LOG4CPLUS_DEBUG(log_dsw, "Destroy CDswutility class!");
}

int CDswutility::OnMessage(EN_MSG_TYPE nMsgType, void * pParam)
{
	switch(nMsgType){
	case PMSG_SET_HDPOWER:
		m_pGPIO->HandleMessage(nMsgType, pParam);
		break;
	case PMSG_SET_VIDEO_ADJ_AUTO:
	case PMSG_SET_AUDIO_PARAM:
		m_pAvCtrl->HandleMessage(nMsgType, pParam);
		break;
	case PMSG_GET_NATTYPE:
		m_pUpnpClient->HandleMessage(nMsgType, pParam);
		break;
	case PMSG_SET_PTZACTION:
		m_pPTZCtrl->HandleMessage(nMsgType, pParam);
		break;
	case PMSG_SET_ACTIVE_INTERFACE:
		m_pTool_network->UpDownNet((const char*)pParam, false);
		break;
	case PMSG_GET_ACTIVE_INTERFACE:
		m_pTool_network->GetActiveInterface((char*)pParam);
		break;
	case PMSG_GET_ACTIVE_IP:
		*((unsigned long*)pParam) = m_pTool_network->GetActiveIp();
		break;
	case PMSG_GET_CURIPMAC:
		m_pTool_network->GetCurIpMac((LPST_CURIP_MAC_T)pParam);
		break;
	case PMSG_SET_RTCTIME:
	case PMSG_GET_RTCTIME:
		m_pDswrtcctrl->HandleMessage(nMsgType, pParam);
		break;
	case PMSG_SET_REBOOT:
		m_pWatchDog->HandleMessage(nMsgType, pParam);
		break;
	case PMSG_GET_DISKINFO:
	case PMSG_SET_DISKFORMAT:
	case PMSG_GET_ISVALIDHD:
	case PMSG_SET_RECPATH:
	case PMSG_SET_HD_FREESPACE:
	case PMSG_GET_DIRLIST:
	case PMSG_SET_UPDATE_TIMEZ:
	case PMSG_GET_TOTALREC_TIMEZ:
	case PMSG_GET_REC_WITH_TIMEZ:
		m_pDisk->HandleMessage(nMsgType, pParam);
		break;
	}
	return 0;
}

